<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>IP情报管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <blockquote class="layui-elem-quote layui-text">
            帮助中心:1. <a href="#" target="_blank">ip情报帮助文档</a>
            <a class="layui-btn" style="float:right" href="javascript:location.replace(location.href);" title="刷新">
              <i class="layui-icon">&#xe669;</i></a>
        </blockquote>
        <fieldset class="table-search-fieldset">
            <legend>搜索</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">IP</label>
                            <div class="layui-input-inline">
                                <input type="text" name="ip" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">自动添加</label>
                            <div class="layui-input-inline">
                              <select name="is_autoAdd">
                                <option value="true">是</option>
                                <option value="false" selected="">否</option>			
                              </select>
                            </div>
                        </div>
<!--
                        <div class="layui-inline">
                            <label class="layui-form-label">是否恶意</label>
                            <div class="layui-input-inline">
                              <select name="is_malicious">
                                <option value="" selected="">全部</option>
                                <option value="true">恶意IP</option>
                                <option value="false">正常IP</option>			
                              </select>
                            </div>
                        </div>
-->
                        <div class="layui-inline">
                            <label class="layui-form-label">标签</label>
                            <div class="layui-input-inline">
                              <select name="tag">
                                <option value="" selected="">全部</option>		
                                <option value="Baiduspider">百度爬虫</option>	
                                <option value="ctyun CDN">天翼云CDN</option>	
                                <option value="Bytespider">字节爬虫</option>	
                                <option value="bingbot">微软爬虫</option>	
                                <option value="YisouSpider">神马爬虫</option>	
                                <option value="Sogouspider">搜狗爬虫</option>	
                                <option value="TrustIP">信任IP</option>
                              </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i>IP情报查询</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-table" id="currentTableId" lay-filter="currentTableFilter"></table>
    </div>
    
    
<!-- 弹添加黑名单框-->
  	<div class="layui-row" id="AddBlackIpTpl" style="display:none;">
    	<div class="layui-col-md11">
      		<form class="layui-form layui-form-pane" action="" lay-filter="CloneVpn">
              <input type="hidden" name="ip" class="layui-input">
              <div class="layui-form-item" pane="">
                  <div class="layui-inline" style="margin-top:10px">
                      <label class="layui-form-label">应用范围</label>
                      <div class="layui-input-inline" style="width:300px;">
                          <input type="text" name="appname" autocomplete="off" class="layui-input" placeholder="分三个级别,例如:sys,baidu,baidu-bbs" lay-verify="required" lay-reqtext="应用名不能为空">
                      </div>
                  </div>
              </div>
				
              <div class="layui-form-item" pane="">
                  <div class="layui-inline">
                      <label class="layui-form-label">封禁时长</label>
                      <div class="layui-input-inline" style="width:300px;">
                          <input type="text" name="expiretime" autocomplete="off" value="0"  placeholder="封禁多少秒" class="layui-input">
                      </div>
                      <div class="layui-form-mid layui-word-aux">秒,0表示永久封禁</div>
                  </div>
              </div>
              <div class="layui-form-item" pane="">
                  <div class="layui-inline">
                      <label class="layui-form-label">备注</label>
                      <div class="layui-input-inline" style="width:300px;">
                          <input type="text" name="remarks" autocomplete="off" class="layui-input">
                      </div>
                  </div>
              </div>		
              <div class="layui-form-item">
                  <div class="layui-inline" style="margin-left:200px">
                      <button type="submit" class="layui-btn"  lay-submit lay-filter="Add"><i class="layui-icon">&#xe61f;</i>确认添加</button>
                  </div>
              </div>
      		</form>
    	</div>
  	</div>
<!-- 弹添加黑名单框end-->
    
</div>
</body>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="add"><i class="layui-icon">&#xe61f;</i>IP情报添加</button>
        <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="addToBlackip"><i class="layui-icon">&#xe61f;</i>选中并添加到黑名单</button>
    </div>
</script>
        
<script type="text/html" id="TagTpl">
    {{# layui.each(d.tag.split(','), function(index, item){
      var html = item;
      switch (item) {
          case "Scanner" :
              html = "扫描";
              break;
          case "C2" :
              html = "远控";
              break;
          case "Botnet" :
              html = "僵尸网络";
              break;
          case "Hijacked" :
              html = "劫持";
              break;
          case "Phishing" :
              html = "钓鱼";
              break;
          case "Malware" :
              html = "恶意软件";
              break;
          case "Exploit" :
              html = "漏洞利用";
              break;
          case "Zombie" :
              html = "傀儡机";
              break;
          case "Spam" :
              html = "垃圾邮件";
              break;
          case "Suspicious" :
              html = "可疑";
              break;
          case "Compromised" :
              html = "失陷主机";
              break;
          case "Whitelist" :
              html = "白名单";
              break;
          case "Brute Force" :
              html = "暴力破解";
              break;
          case "Proxy" :
              html = "代理";
              break;
          case "SQLI" :
              html = "sql注入";
              break;
          case "Info" :
              html = "基础信息";
              break;
          case "Baiduspider" :
              html = "百度爬虫";
              break;
          case "Bytespider" :
              html = "字节爬虫";
              break;
          case "bingbot" :
              html = "微软爬虫";
              break;
          case "YisouSpider" :
              html = "神马爬虫";
              break;
          case "Sogouspider" :
              html = "搜狗爬虫";
              break;
          default :
              html = item;
      }
    }}
        <span class="layui-badge layui-bg-blue">{{ html }}</span>
    {{# });  }}
</script>	

<script type="text/html" id="statusTpl">
    {{# if (d.is_malicious == 'true') { }}
      <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete">是</a>
    {{# } else { }}
      <a class="layui-btn layui-btn-xs">否</a>
    {{# } }}
</script>

<script type="text/html" id="limitRuleTpl2">
    {{# return JSON.stringify(d.limit_rule) }}
</script>

<script>
    function getQueryString(name) {
      let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
      let r = window.location.search.substr(1).match(reg);
      if (r != null) {
          return unescape(r[2]);
      };
      return null;
    };
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

//var stype = getQueryString("ruletype");
        table.render({
            elem: '#currentTableId',
            url: '/xwaf/riskip/v1/List',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter'],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id',width:60,  title: 'ID', sort: true},
                {field: 'ip', title: 'IP'},
                {field: 'is_malicious',title:'恶意',edit:'text',sort: true,templet: '#statusTpl'}, 
                {field: 'tag',width:'35%',edit:'text', title: '标签', sort: true,templet: '#TagTpl'}, 
                {field: 'risk_type',width:100,edit:'text',title: '类型'},
                {field: 'city',width:250,title: '城市'},
                {field: 'remarks',title: '备注',edit:'text'},
                {field: 'createtime', title: '创建时间'},
                {field: 'updatetime', title: '更新时间'},
            ]],
            limits: [25, 50, 100],
            limit: 25,
            page: true
        });

        table.on('toolbar(currentTableFilter)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: 'ip情报添加',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['55%', '92%'],
                    content: '/page/xwaf/waf_add_riskip.html',
                    end: function(){
                    	window.location.reload();//关闭open打开的页面时，刷新父页面
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'addToBlackip') {  // 监听删除操作
          				var data = checkStatus.data;			
          	  			if(data.length > 0){                                            
                      var list='';
            	  			console.log('选中 '+data.length+' 行');
            	  			for (var i in data){
                          		//ids.push(data[i].id);
            			  		    list+=data[i].ip+',';
                          	console.log(data[i].ip);
                     		}
            	  			ipstr = list.substr(0,list.length-1);

          					  layer.open({
                      			type:1,
          						      skin: 'layui-layer-rim',
                      			title:"添加到黑名单",
                      			area: ['620px','350px'],
                      			content:$("#AddBlackIpTpl").html()
                  		});

                  		form.on('submit(Add)', function(datas){	
                        datas.field.ip = ipstr;
          						  console.log(datas.field);                        
          			    		if (datas.field.appname == ""){
                            console.log(datas.field.appname);
          							  layer.msg('应用范围不能为空', {icon: 2})
          						  }
          						  if (datas.field.expiretime < 0){
          							  layer.msg('数量必须是大于等于0的整数', {icon: 2})
          						  }				  
          	    				layer.confirm('确定添加黑名单选中的'+ipstr+'?', {icon: 3, title:'提示',yes: function(index){
                      				$.ajax({
                          				url: "/xwaf/blackip/v1/Add",
                          				type: "POST",
                          				data: JSON.stringify(datas.field),
                          				dataType: "json",
                          				success: function (data) {
                             					if(data.code == 0){
                                 					layer.msg('添加成功', {icon: 1})
                                          //window.location.href = "/page/xwaf/waf_blackip_admin.html"
                                          window.open("/page/xwaf/waf_blackip_admin.html")
          					   					          //console.log(data);
                                 					//location.reload();
                             					}else{
                                 					layer.msg('添加失败',{icon:2})
                                 					//location.reload();
                             					}
                          				}
                      				});		  	  
                  				},
                  				cancel: function(index, layero){ 
                          				layer.close(index);
          										    console.log("不操作");
                  							} 
                 					}); 				
          	   					  return false; 
                			});	
          	  			}else{
          	    			console.log("未勾选");
          					  layer.msg('未勾选', {icon: 2})
          	  			}								
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            //layer.alert(result, {
            //    title: '最终的搜索信息'
            //});
            data.field.ip = data.field.ip.replace(/^\s*|\s*$/g,"");
            data.field.is_malicious = "";
            //执行搜索重载
            table.reload('currentTableId', {
                //page: {
                //    curr: 1,
                //    limit:50
                //},
                method:'POST',
                url: '/xwaf/riskip/v1/List',
                contentType: 'application/json',
                where: data.field,
                done: function(res, curr, count){
                  if (res.code != 0) {
                    layer.open({
                      area: ['420px', '240px'], // 宽高
                      type: 1,
                      shade: 0.2,
                      shadeClose: true, // 点击遮罩关闭层
                      content:['<div style="padding: 16px;">是否恶意：<button class="layui-btn layui-btn-xs layui-btn-danger">'+res.is_malicious+'</button></div>',
                          '<div style="padding: 16px;">标签：<button class="layui-btn layui-btn-xs">'+res.tag+'</button></div>'].join(''),
                      yes:function(index){
                          layer.close(index);
                          window.location.reload();
                      },
                      cancel: function(index){
                          layer.close(index);
                          window.location.reload();
                      }                                                       
                    });
                  }
                  //console.log(res);
                  //this.where={};
                  //不清空，有问题：搜索条件会保留
                  //清空，  有问题：分页的时候没有搜索条件了,分页会受到影响
                  //https://copyfuture.com/blogs-details/20200729003153292ltjtf01kvp048gr
                }
            }, 'data');

            return false;
        });

  	//监听单元格编辑
  	table.on('edit(currentTableFilter)', function(obj){
    	var value = obj.value //得到修改后的值
    	,datas = obj.data //得到所在行所有键值
    	,field = obj.field; //得到字段

      var bodystr = {"id":datas.id,"name":field,"value":value};
			      layer.confirm('确定修改?', {icon: 3, title:'提示',yes: function(index){
                 	$.ajax({
                     	//url: "/xwaf/rule/v1/Edit?id="+datas.id,
                     	url: "/xwaf/riskip/v1/Edit?id=" + datas.id,
                      type: "POST",
                      data: JSON.stringify(bodystr),
                      dataType: "json",
                     	success: function (resp) {
                         	if(resp.code == 0){
                            	layer.msg('['+ datas.id +'] ' + field + '更改为:'+ value);
                         	}else{
                            	layer.msg('['+ datas.id +'] ' + field + '更改失败',{icon:2});
                         	}
                    	}
                	});	
              },
              cancel: function(index, layero){ 
                  layer.close(index);
              } 
            }); 	
  	});
  });
</script>
</html>